﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using System.Diagnostics;

namespace SimpleIR
{
	public sealed class IRT1T2Block : IRT1T2Region
	{
		private readonly List<IRT1T2Region> m_T1T2List;

		public IRT1T2Block(List<IRT1T2Region> bl)
		{ this.m_T1T2List = bl; }

		public override String ToString()
		{
			StringBuilder sb = new StringBuilder();
			foreach(IRT1T2Region bb in this.m_T1T2List)
				sb.Append(bb.ToString());
			return sb.ToString();
		}

		public override void DGMLDisplayString(XmlWriter xwn, XmlWriter xwl, bool toplevel)
		{
			if(!toplevel)
			{
				xwn.WriteStartElement("Node");
				xwn.WriteAttributeString("Id", this.GenDisplayNameString());
				xwn.WriteAttributeString("Label", "Block -- [" + this.MinStmt().ToString() + ", " + this.MaxStmt().ToString() + "]");

				xwn.WriteAttributeString("Category", "Summary");
				xwn.WriteAttributeString("Group", "Collapsed");
				xwn.WriteEndElement();
			}

			foreach(IRT1T2Region bb in this.m_T1T2List)
			{
				bb.DGMLDisplayString(xwn, xwl, false);

				if(!toplevel)
				{

					xwl.WriteStartElement("Link");
					xwl.WriteAttributeString("Source", this.GenDisplayNameString());
					xwl.WriteAttributeString("Target", bb.GenDisplayNameString());
					xwl.WriteAttributeString("Category", "Contains");
					xwl.WriteEndElement();
				}
			}
		}

		public override string GenDisplayNameString()
		{ return "b" + this.m_T1T2List[0].GenDisplayNameString(); }

		public override void NumberRegion(CtrGen cg)
		{
			foreach(IRT1T2Region at in this.m_T1T2List)
				at.NumberRegion(cg);
		}

		public override int MinStmt()
		{
			int mv = Int32.MaxValue;
			foreach(IRT1T2Region fa in this.m_T1T2List)
				mv = Math.Min(mv, fa.MinStmt());
			return mv;
		}

		public override int MaxStmt()
		{
			int mv = Int32.MinValue;
			foreach(IRT1T2Region fa in this.m_T1T2List)
				mv = Math.Max(mv, fa.MaxStmt());
			return mv;
		}

		/// <summary>
		/// Return a list of all the T1T2Regions in this block.
		/// </summary>
		public List<IRT1T2Region> T1T2Blocks
		{ get { return this.m_T1T2List; } }
	}
}
